

<template>
  <PlusDialogForm
      ref="plusFormInstance"
      v-model:visible="dialogShow"
      v-model="formData"
      :form="{
        columns,
        labelWidth:'110px',
        labelPosition:'right',
        rules:formRules,
        rowProps:{ gutter: 20 },
        colProps:{  span:12}
      }"
      :dialog="{ title: dialogTitle, width: '800px', top: '12vh', confirmLoading }"
      @confirm="handleSubmit"
      @cancel="handleCancel"
  >
    <template #plus-field-coverImg>
      <image-upload limit="1" :is-show-tip="false" v-model="formData.coverImg" />
    </template>
  </PlusDialogForm>
</template>


<script setup>
import { getSurveyApi, addSurveyApi, updateSurveyApi } from "@/api/feedback/survey";
import { ref } from "vue"
import {usePageCommonHook} from "./index.js";
const {
  sys_yes_no,
  fed_survey_type,
  proxy,
  columns,
  primaryKey
} = usePageCommonHook(true);


const emit = defineEmits(["update"]);

const dialogShow = ref(false);
const confirmLoading = ref(false)
const isEdit = computed(() =>!!formData.value[primaryKey])
const dialogTitle = computed(() => (isEdit.value ? '编辑' : '新增')+'问卷')
const plusFormInstance = ref(null);
const formInstance = computed(()=>plusFormInstance.value?.formInstance)


const formData = ref({
  surName: "",
  surDesc: "",
  coverImg: "",
  startTime: "",
  endTime: "",
  surType:'',
  dateTimeRange:[],
  isPublic: "N",
  repeatJoin: "N",
  surFinish: "",
  remark: "",
});
const formRules = ref({
  surName: [{ required: true, message: "问卷名称不为空", trigger: ["blur","change"] }],
  dateTimeRange: [
      { required: true,
        validator: (rule, value, callback) => {
          if(value.length === 0){
            return callback(new Error('起止时间不能为空'))
          }
          if(!value[0]){
            return callback(new Error('开始时间不为空'))
          }
          if(!value[1]){
            return callback(new Error('结束时间不为空'))
          }
          callback()
        },
        trigger: ["blur","change"] }],
  isPublic: [{ required: true, message: "是否公开不能为空", trigger: ["blur","change"] }],
  repeatJoin: [{ required: true, message: "点击提交后是否可以再次参与不能为空", trigger: ["blur"] }],
  coverImg: [{ required: true, message: "封面图片不能为空", trigger: ["blur"] }]
});

// 确定按钮操作
const handleSubmit = async () => {
  try {
    confirmLoading.value = true
    const { dateTimeRange, ...data } = formData.value;
    data.startTime = dateTimeRange[0]
    data.endTime = dateTimeRange[1]
    if (isEdit.value) {
      await updateSurveyApi(data)
      proxy.$modal.msgSuccess("修改成功")
    } else {
      await addSurveyApi(data)
      proxy.$modal.msgSuccess("新增成功")
    }
    handleCancel();
    emit('update')
  } catch (error) {
    console.warn('提交错误:', error)
  } finally {
    confirmLoading.value = false
  }
}

// 取消按钮操作
function handleCancel() {
  dialogShow.value = false
  onReset();
}

// 重置数据
const onReset = () => {
  formData.value = {
    surName: "",
    surDesc: "",
    coverImg: "",
    startTime: "",
    endTime: "",
    surType:'',
    dateTimeRange:[],
    isPublic: "N",
    repeatJoin: "N",
    surFinish: "",
    remark: "",
  }
  formInstance.value.resetFields();
}
// 查询回显
const getDetail = async (key) => {
  const { data } = await  getSurveyApi(key)
  data.dateTimeRange = [data.startTime, data.endTime]
  formData.value = data;
}
// 打开弹框
const openDialog = async (row) => {
  if(row && row[primaryKey]){
    await getDetail(row[primaryKey])
  }
  dialogShow.value = true;
};
defineExpose({ openDialog });
</script>


